<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Validation extends CI_Form_validation {

    public function __construct($rules = array())
	{
	    parent::__construct($rules);
	}
	
	public function is_unique($value, $params)	{

	    $fields = explode(",", $params);
        list($table, $field) = explode('.', $fields[0], 2);
        
        $this->CI->db->select($field)
                     ->from($table)
                     ->where($field, $value)
                     ->limit(1);

        if (isset($fields[1])) {
            list($where_table, $where_field) = explode('.', $fields[1], 2);
            $where_value = $this->CI->input->post($where_field);
            if (isset($where_value)) {
                $this->CI->db->where("{$where_table}.{$where_field} <>", $where_value);
            }
        }
        $query = $this->CI->db->get();
        if ($query->num_rows) {
            return false;
        }
        return true;
	}
}
